<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>计算字串出现的次数</title>
 </head>
 <body class="docs"><div id="layout">
  <div id="layout-content"><div id="function.substr-count" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">substr_count</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">substr_count</span> &mdash; <span class="dc-title">计算字串出现的次数</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.substr-count-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>substr_count</strong></span>
    ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$haystack</code></span>
   , <span class="methodparam"><span class="type">string</span> <code class="parameter">$needle</code></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$offset</code><span class="initializer"> = 0</span></span>
   [, <span class="methodparam"><span class="type">int</span> <code class="parameter">$length</code></span>
  ]] ) : <span class="type">int</span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>substr_count()</strong></span> 返回子字符串<code class="parameter">needle</code> 在字符串 <code class="parameter">haystack</code> 中出现的次数。注意 <code class="parameter">needle</code> 区分大小写。
  </p>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    该函数不会计算重叠字符串。参见下面的例子。
   </p>
  </p></blockquote>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.substr-count-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    
     <dt>
<code class="parameter">haystack</code></dt>

     <dd>

      <p class="para">
       在此字符串中进行搜索。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">needle</code></dt>

     <dd>

      <p class="para">
       要搜索的字符串。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">offset</code></dt>

     <dd>

      <p class="para">
       开始计数的偏移位置。如果是负数，就从字符的末尾开始统计。
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">length</code></dt>

     <dd>

      <p class="para">
       指定偏移位置之后的最大搜索长度。如果偏移量加上这个长度的和大于 <code class="parameter">haystack</code> 的总长度，则打印警告信息。
       负数的长度 length 是从 <code class="parameter">haystack</code> 的末尾开始统计的。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.substr-count-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  该函数返回<span class="type"><span class="type 整型">整型</span></span>。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.substr-count-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        开始支持负数的 <code class="parameter">offset</code> 和 <code class="parameter">length</code>。
       </td>
      </tr>

      <tr>
       <td>5.1.0</td>
       <td>
        新增 <code class="parameter">offset</code> 和 <code class="parameter">length</code> 参数。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.substr-count-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-5992">
    <p><strong>Example #1 <span class="function"><strong>substr_count()</strong></span> 范例</strong></p>
    <div class="example-contents">
<div class="phpcode"><pre><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'This&nbsp;is&nbsp;a&nbsp;test'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;14<br /><br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;2<br /><br />//&nbsp;字符串被简化为&nbsp;'s&nbsp;is&nbsp;a&nbsp;test'，因此输出&nbsp;1<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;字符串被简化为&nbsp;'s&nbsp;i'，所以输出&nbsp;0<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">3</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;因为&nbsp;5+10&nbsp;&gt;&nbsp;14，所以生成警告<br /></span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'is'</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">5</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">);<br /><br /><br /></span><span style="color: #FF8000">//&nbsp;输出&nbsp;1，因为该函数不计算重叠字符串<br /></span><span style="color: #0000BB">$text2&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'gcdgcdgcd'</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">substr_count</span><span style="color: #007700">(</span><span style="color: #0000BB">$text2</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'gcdgcd'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</pre></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.substr-count-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="count_chars.html" class="function" rel="rdfs-seeAlso">count_chars()</a> - 返回字符串所用字符的信息</span></li>
    <li class="member"><span class="function"><a href="strpos.html" class="function" rel="rdfs-seeAlso">strpos()</a> - 查找字符串首次出现的位置</span></li>
    <li class="member"><span class="function"><a href="substr.html" class="function" rel="rdfs-seeAlso">substr()</a> - 返回字符串的子串</span></li>
    <li class="member"><span class="function"><a href="strstr.html" class="function" rel="rdfs-seeAlso">strstr()</a> - 查找字符串的首次出现</span></li>
   </ul>
  </p>
 </div>

</div></div></div></body></html>